package ext.tianma.model.util;

import com.ptc.core.foundation.type.server.impl.TypeHelper;
import com.ptc.core.meta.common.TypeIdentifier;

import wt.change2.WTChangeOrder2;
import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.part.WTPart;
import wt.type.TypeDefinitionReference;
import wt.type.TypedUtility;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import ext.tianma.model.TMECOITEMModel;

public class TMObjectUtilHelper {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			createTMECOITEMModel();
		} catch (WTPropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static TMECOITEMModel createTMECOITEMModel() throws WTException, WTPropertyVetoException{
		TMECOITEMModel TMECOItem = null;
		
		String strObjType = TMECOITEMModel.class.getName();
		System.out.println("strObjType:"+strObjType);
		TypeDefinitionReference typeDefinitionRef = TypedUtility
				.getTypeDefinitionReference(strObjType);
		TypeIdentifier typeidentifier = TypeHelper
				.getTypeIdentifier(strObjType);
		
		String number = getTMECOITEMAutoNumber();
		
		WTChangeOrder2 ecn = WTChangeOrder2.newWTChangeOrder2();
		WTPart part = WTPart.newWTPart();

		TMECOItem = TMECOITEMModel.newTMECOITEMModel();
		/**
		 * 设置对象属性
		 */
		TMECOItem.setNumber(number);
		
		TMECOItem.setECOAffectedRef(ObjectReference.newObjectReference(part));
		TMECOItem.setECORef(ObjectReference.newObjectReference(ecn));
		
		TMECOItem = (TMECOITEMModel) PersistenceHelper.manager.save(TMECOItem);
		
		return TMECOItem;
		
	}
	
	/**
	 * 自动生成编号
	 * 
	 * @return
	 * @throws WTException
	 */
	public static String getTMECOITEMAutoNumber() throws WTException {
		String strNumber = PersistenceHelper.manager
				.getNextSequence("TMECOITEM_seq");
		System.out.println("TMECOITEM_seq......................:" + strNumber);
		while (strNumber.length() < 10) {
			strNumber = "0" + strNumber;
		}
		strNumber = "TMECOITEM_" + strNumber;
		System.out.println("return.....................:" + strNumber);
          return strNumber;
	}
	
}
